$(document).ready(function() {
	
	/* set bg of page to color the page according to the style in ui-css*/
	$('body').addClass("yvo-body-content");
	$('html').addClass("yvo-body-content");
	
	/* set other css properties if present */

	
	/* rest of the scripts */
    changeEmailTemplate();
    
    var day_interval_tabs = $("#day_interval_tabs").tabs();

    $("input[id^='add_day_interval_']").live('click', function() {
        $.ajax({
            type: "GET",
            url: "/settings/day-interval/add",
            dataType: 'html',
            success: function (response) {
                var dialog_buttons = {};

                /* handler for day interval submission */
                dialog_buttons['OK'] = function() {
                    var day_selected = day_interval_tabs.tabs('option', 'selected') + 1;
                    var data = $("#day_interval_form").serialize() + '&day=' + day_selected;
                    
                    $.ajax({
                        type: "POST",
                        url: $('#day_interval_form').attr('action'),
                        data: data,
                        dataType: 'json',
                        success: function (response) {
                            if (0 == response.error) {
                                $('#dialog_common').dialog('close');
                                $('#tabs-' + day_selected).load('/settings/get-day-intervals/' + day_selected );
                            }
                            else {
                                $("#dialog_common").html(response.content);
                            }
                        }
                    });
                };

                dialog_buttons['Cancel'] = function(){$(this).dialog('close');};

                var settings = {
                    title: "Add new entry",
                    modal : true,
                    draggable: true,
                    width : "auto",
                    height: "auto",
                    position: 'center',
                    stack: true,
                    autoOpen : false,
                    closeOnEscape: false,
                    resizable: false,
                    autoResize:true,
                    buttons: dialog_buttons
                };

                $("#dialog_common").html(response);
                $("#dialog_common").dialog(settings);
                $("#dialog_common").dialog("open");
            }
        });
    });

    $("a[id^='interval_delete_']").live('click', function(event) {
        event.preventDefault();
        var url = $(this).attr('href');

        jConfirm('Are you sure you want to delete the time interval?', 'Delete time interval', function(response) {
            if (response) {
                $.ajax({
                    type: "POST",
                    url: url,
                    dataType: 'json',
                    success: function () {
                        var day_selected = day_interval_tabs.tabs('option', 'selected') + 1;
                        $('#tabs-' + day_selected).load('/settings/get-day-intervals/' + day_selected );
                    }
                });
            }
        });
    });

    $("#add_block_time").live('click', function() {
        $.ajax({
            type: "GET",
            url: "/settings/block-time/add",
            dataType: 'html',
            success: function (response) {
                var dialog_buttons = {};

                /* handler for day interval submission */
                dialog_buttons['OK'] = function() {
                    $.ajax({
                        type: "POST",
                        url: $('#block_time_form').attr('action'),
                        data: $("#block_time_form").serialize(),
                        dataType: 'json',
                        success: function (response) {
                            if (0 == response.error) {
                                $('#dialog_common').dialog('close');
                                location.reload();
                            }
                            else {
                                $("#dialog_common").html(response.content);
                                set_datepicker('#block_time_date_start');
                                set_datepicker('#block_time_date_end');
                            }
                        }
                    });
                };

                dialog_buttons['Cancel'] = function(){$(this).dialog('close');};

                var settings = {
                    title: "Add new entry",
                    modal : true,
                    draggable: true,
                    width : "auto",
                    height: "auto",
                    position: 'center',
                    stack: true,
                    autoOpen : false,
                    closeOnEscape: false,
                    resizable: false,
                    autoResize:true,
                    buttons: dialog_buttons
                };

                $("#dialog_common").html(response);
                $("#dialog_common").dialog(settings);
                $("#dialog_common").dialog("open");

                set_datepicker('#block_time_date_start');
                set_datepicker('#block_time_date_end');
            }
        });
    });

    $("a[id^='block_time_delete_']").live('click', function(event) {
        event.preventDefault();
        var url = $(this).attr('href');
        
        jConfirm('Are you sure you want to delete the block time?', 'Delete block time', function(response) {
            if (response) {
                $.ajax({
                    type: "POST",
                    url: url,
                    success: function () {
                        location.reload();
                    }
                });
            }
        });
    });
    

    $("#add_special_date").click(function() {
        $.ajax({
            type: "GET",
            url: "/settings/special-date/add",
            dataType: 'html',
            success: function (response) {
                var dialog_buttons = {};

                /* handler for add new special date */
                dialog_buttons['OK'] = function() {
                    $.ajax({
                        type: "POST",
                        url: $('#special_date_form').attr('action'),
                        data: $("#special_date_form").serialize(),
                        dataType: 'json',
                        success: function (response) {
                            if (0 == response.error) {
                                $('#dialog_common').dialog('close');
                                document.location.href = '/settings/index';
                            }
                            else {
                                $("#dialog_common").html(response.content);
                                set_datepicker('#special_date_date');
                            }
                        }
                    });
                };

                dialog_buttons['Cancel'] = function(){$(this).dialog('close');};

                var settings = {
                    title: "Add new special date",
                    modal : true,
                    draggable: true,
                    width : "auto",
                    height: "auto",
                    position: 'center',
                    stack: true,
                    autoOpen : false,
                    closeOnEscape: false,
                    resizable: false,
                    autoResize:true,
                    buttons: dialog_buttons
                };

                $("#dialog_common").html(response);
                $("#dialog_common").dialog(settings);
                $("#dialog_common").dialog("open");

                set_datepicker('#special_date_date');
            }
        });
    });

    $("a[id^='special_date_delete_']").click(function(event) {
        event.preventDefault();
        var url = $(this).attr('href');

        jConfirm('Are you sure you want to delete the special date?', 'Delete special date', function(response) {
            if (response) {
                $.ajax({
                    type: "POST",
                    url: url,
                    success: function () {
                        document.location.href = '/settings/index';
                    }
                });
            }
        });
    });

    $('#special_date_no').blur(function(event) {
        $.ajax({
            type: "POST",
            url: '/settings/special-date/set-count/' + $(this).val(),
            success: function () {
                
            }
        });
    });
    
    $('#email_message_header_footer_button').live('click', function() {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: $("#email_message_header_footer_form").attr('action'),
            data: $("#email_message_header_footer_form").serialize(),
            success: function (response) {
                if ('0' == response.error)
                    alert('Email header and footer saved');
                
                $('#email_message_header_footer_wrapper').html(response.content);
            }
        });
    });
    
    $('#html_template_header_footer_button').live('click', function() {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: $("#html_template_header_footer_form").attr('action'),
            data: $("#html_template_header_footer_form").serialize(),
            success: function (response) {
                if ('0' == response.error){
                    alert('HTML header and footer saved');
                }else{
                	alert('HTML header and footer not saved');
                }
                
                $('#email_message_header_footer_wrapper').html(response.content);
            }
        });
    });

    $('#group_reservation_size').live('blur', function() {
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: '/settings/group-reservation-size/save',
            data: { size: $(this).val() },
            success: function (response) {
                if ('1' == response.error)
                    alert(response.content);
            }
        });
    });

    $("input[id^='add_group_menu']").live('click', function() {
        $.ajax({
            type: "GET",
            url: "/settings/group-menu/add",
            dataType: 'html',
            success: function (response) {
                var dialog_buttons = {};

                /* handler for day interval submission */
                dialog_buttons['OK'] = function() {
                    $.ajax({
                        type: "POST",
                        url: $('#group_menu_form').attr('action'),
                        data: $("#group_menu_form").serialize(),
                        dataType: 'json',
                        success: function (response) {
                            if (0 == response.error) {
                                $('#dialog_common').dialog('close');
                                document.location.href = '/settings/index';
                            }
                            else {
                                $("#dialog_common").html(response.content);
                            }
                        }
                    });
                };

                dialog_buttons['Cancel'] = function(){$(this).dialog('close');};

                var settings = {
                    title: "Add new group menu",
                    modal : true,
                    draggable: true,
                    width : "auto",
                    height: "auto",
                    position: 'center',
                    stack: true,
                    autoOpen : false,
                    closeOnEscape: false,
                    resizable: false,
                    autoResize:true,
                    buttons: dialog_buttons
                };

                $("#dialog_common").html(response);
                $("#dialog_common").dialog(settings);
                $("#dialog_common").dialog("open");
            }
        });
    });


    $("a[id^='group_menu_delete_']").click(function(event) {
        event.preventDefault();

        jConfirm('Are you sure you want to delete the group menu?', 'Delete group menu', function(response) {
            if (response) {
                $.ajax({
                    type: "POST",
                    url: url,
                    success: function () {
                        document.location.href = '/settings/index';
                    }
                });
            }
        });
    });
    
    /* group menu edit */
    $("a[id^='group_menu_edit_']").click(function(event) {
        event.preventDefault();
        var groupMenuId= $(this).attr("id").replace('group_menu_edit_', '');

        $.ajax({
            type: "GET",
            url: '/settings/group-menu/edit/' + groupMenuId,
            success: function (response) {
                var dialog_buttons = {};

                /* handler for day interval submission */
                dialog_buttons['OK'] = function() {
                    $.ajax({
                        type: "POST",
                        url: $('#group_menu_edit_form').attr('action'),
                        data: $("#group_menu_edit_form").serialize(),
                        dataType: 'json',
                        success: function (response) {
                            if (0 == response.error) {
                                $('#dialog_common').dialog('close');
                                document.location.href = '/settings/index';
                            }
                            else {
                                $("#dialog_common").html(response.content);
                            }
                        }
                    });
                };

                dialog_buttons['Cancel'] = function(){$(this).dialog('close');};

                var settings = {
                    title: "Edit the group menu",
                    modal : true,
                    draggable: true,
                    width : "auto",
                    height: "auto",
                    position: 'center',
                    stack: true,
                    autoOpen : false,
                    closeOnEscape: false,
                    resizable: false,
                    autoResize:true,
                    buttons: dialog_buttons
                };

                $("#dialog_common").html(response);
                $("#dialog_common").dialog(settings);
                $("#dialog_common").dialog("open");
            }
        });
    });

    /* handler for saving a template */
    $('#save_template').live('click', function() {
        $.ajax({
            type: "POST",
            url: '/settings/email-template/save',
            data: { 
                body: $('#email_temaplate_body').val(),
                subject: $('#email_temaplate_subject').val(),
                category: $('#email_template_category option:selected').attr('id')
            },
            success: function (response) {
                alert(response);
            }
        });
    });
    
    /* handler for saving a template */
    $('#save_guest_settings').live('click', function() {
        $.ajax({
            type: "POST",
            url: '/settings/guest_settings/save',
            data: { 
        	reservation_min_time: $('#reservation_min_time').val(),
        	reservation_min_time_msg: $('#reservation_min_time_msg').val(),
        	reservation_min_time_grp: $('#reservation_min_time_grp').val(),
        	reservation_min_time_grp_msg: $('#reservation_min_time_grp_msg').val()
        	
            },
            success: function (response) {
                alert(response);
            }
        });
    });
    
    
    

    /* get a template from the server */
    $('#email_template_category').live('change', function() {
        changeEmailTemplate();
    });
});